package com.wx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wx.domain.Bill;
import com.wx.service.BillService;
import com.wx.service.OrderService;
import com.wx.service.impl.BillServiceImpl;
import com.wx.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/addBillServlet")
public class AddBillServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");
        ObjectMapper mapper=new ObjectMapper();
        Map<String, Object> map=new HashMap<>();

        String orderId=req.getParameter("orderId");
        String doctorId = req.getParameter("doctorId");
        String patientId = req.getParameter("patientId");
        String billAll = req.getParameter("billAll");
        double money= Double.parseDouble(req.getParameter("money"));
        System.out.println(doctorId+patientId+billAll+money);
        Bill bill=new Bill();
        bill.setDoctorId(Integer.parseInt(doctorId));
        bill.setPatientId(Integer.parseInt(patientId));
        bill.setBillAll(billAll);
        bill.setMoney(money);

        System.out.println(bill.toString());

        bill.setPayment("未支付");
        BillService billService=new BillServiceImpl();
        int id = billService.addBill(bill);
        OrderService orderService=new OrderServiceImpl();
        orderService.updateHandle(id,orderId);
        map.put("id",id);
        mapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
